package com.compty.security.web;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.compty.core.page.Page;
import com.compty.core.struts2.BaseAction;
import com.compty.security.domain.Resc;
import com.compty.security.manager.RescManager;


/**
 * resc action.
 *
 */
public class RescAction extends BaseAction<Resc> {
    /** * serial. */
    static final long serialVersionUID = 0L;

    /** * resc manager. */
    private transient RescManager rescManager = null;

    /** * resc list. */
    private List<Resc> list;

    /** * id. */
    private long id;

    // ====================================================
    private String ids;
    private Resc resc;

    /** * @return success. */
    public String list() {
//        this.list = rescManager.getAll();
        // 分页
        Criteria criteria;

        if (sort != null) {
            boolean isAsc = dir.equalsIgnoreCase("asc");
            criteria = rescManager.createCriteria(sort, isAsc);
        } else {
            criteria = rescManager.createCriteria();
        }

        if ((filterTxt != null) && (filterValue != null)&& (!filterTxt.equals("")) && (!filterValue.equals(""))) {
            criteria = criteria.add(Restrictions.like(filterTxt,
                        "%" + filterValue + "%"));
        }

        this.page = rescManager.pagedQuery(criteria, curPage, pageSize);

        return SUCCESS;
    }

    /** * @return forward. */
    public String edit() {
        this.resc = rescManager.get(id);

        return "addPrepare";
    }

    //
    /** * @param rescManager resc manager. */
    public void setRescManager(RescManager rescManager) {
        this.rescManager = rescManager;
    }

    /** * @return resc list. */
    public List<Resc> getList() {
        return list;
    }

    /** * @param id long. */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * 分页浏览记录.
     *
     * @throws Exception 异常
     */
    public void pagedQuery() throws Exception {
        // 分页
        Criteria criteria;

        if (sort != null) {
            boolean isAsc = dir.equalsIgnoreCase("asc");
            criteria = rescManager.createCriteria(sort, isAsc);
        } else {
            criteria = rescManager.createCriteria();
        }

        if ((filterTxt != null) && (filterValue != null)
                && (!filterTxt.equals("")) && (!filterValue.equals(""))) {
            criteria = criteria.add(Restrictions.like(filterTxt,
                        "%" + filterValue + "%"));
        }

        Page page = rescManager.pagedQuery(criteria, curPage, pageSize);
    }

    /**
     * 保存，新增或修改.
     *
     * @throws Exception 异常
     */
    public String save() throws Exception {
    	
        if(this.resc != null){
        	rescManager.save(resc);
        }
        
        return "redirect";
    }
    
    public String addPrepare(){
    	
    	this.resc = null;
    	return "addPrepare";
    }
    
    

    /**
     * 读取数据.
     *
     * @throws Exception 异常
     */
    public void loadData() throws Exception {
        Resc entity = rescManager.get(id);

        if (entity != null) {
            List<Resc> list = new ArrayList<Resc>();
            list.add(entity);
        }
    }

    /**
     * 删除记录.
     *
     * @throws Exception 异常
     */
    public String remove() throws Exception {
        for (String str : ids.split(",")) {
            try {
                long id = Long.parseLong(str);
                rescManager.removeById(id);
            } catch (NumberFormatException ex) {
                continue;
            }
        }

        return "redirect";
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

	public Resc getResc() {
		return resc;
	}

	public void setResc(Resc resc) {
		this.resc = resc;
	}

}
